<?php

namespace App\Http\Controllers;

use App\Models\AddressModel;
use App\Models\HouseAudit;
use App\Services\conditionService;
use App\Services\ElasticsearchService;
use App\Services\HouseServices;
use App\Services\RedisGeo;
use App\Services\StatusNumber;
use App\Tools\JsonTools;
use App\Tools\TxdtTools;
use Illuminate\Support\Facades\Request;
use QL\QueryList;


class HouseController extends Controller
{

    /**
     * @Notes:
     * @Author: 遇见
     * @Date: 2022/11/14
     * @Time: 14:27
     * @return array
     * 房源类型展示
     */
    public function houseType()
    {
        $typeModel = HouseServices::houseType();
        return $typeModel;
    }

    /**
     * @Notes:
     * @Author: 遇见
     * @Date: 2022/11/14
     * @Time: 14:26
     * @return array
     * 房源求租求购添加
     */
    public function houseAdd()
    {
        $param = \request()->post();
        $uid = \request()->id;
        $typeModel = HouseServices::houseAddService($param, $uid);
        return $typeModel;
    }

    /**
     * @Notes:
     * @Author: 遇见
     * @Date: 2022/11/14
     * @Time: 14:25
     * @return array
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     * 获取求租求购房源列表
     */
    public function listHome()
    {
        $param = \request()->get('id');
        $address = request()->input('address', "");
        $type = request()->input('type', "");
        $region = request()->input('region', "");
        $listModel = (new HouseServices())->listModel($param, $address, $type, $region);
        return $listModel;
    }

    /**
     * @Notes:房源爬取
     * @Author:Planb,
     * @Date: 2022/11/14,
     * @Time: 14:24,
     */
    public function queryHouseList()
    {
        $url = 'https://shanghai.anjuke.com/sale/?pi=baidu-cpc-sh-cty1&kwid=519731136&bd_vid=11943663895643621348';
        $range = '.list .property a';
        $rules = [
            'house_price' => ['.property-content .property-price .property-price-average', 'text'],
            'house_name' => ['.property-content .property-content-detail .property-content-title', 'text'],
            'house_logo' => ['.property-image img', 'src']
        ];
        $data = QueryList::get($url)->rules($rules)->range($range)->queryData();
        HouseAudit::insert($data);
    }

    /**
     * @Notes:小程序展示二手房源列表
     * @Author:Planb,
     * @Date: 2022/11/14,
     * @Time: 14:24,
     */
    public function getHouseList()
    {
        $limit = request()->get('limit', 5);
        $page = request()->get('page', 1);
        $search = request()->get('search');
        $city = request()->get('city');
        $offset = intval(($page - 1)) * intval($limit);
        if ($search) {
            $result = (ElasticsearchService::getInstance())->searchDocument('house_list', $limit, $offset, 'house_name', $search);
            if (!$result) {
                return JsonTools::JsonError([], 8002, '搜索无数据');
            }
            return JsonTools::JsonSuccess($result, 200, 'ok');
        }

        $houseList = (new HouseServices())->getHouseListServer($limit, $offset, $city);
        if ($houseList) {
            return JsonTools::JsonSuccess($houseList, 200, 'ok');
        } else {
            return JsonTools::JsonError([], 8001, '列表获取失败');
        }
    }

    /**
     * @Notes:
     * @Author: 遇见
     * @Date: 2022/11/15
     * @Time: 19:40
     * 获取用户当前位置
     */
    public function userAddress()
    {
        $param = request()->post();
        $address = TxdtTools::staticAddressComponent($param['latitude'] . ',' . $param['longitude']);
        $pid = AddressModel::where('name', $address)->first();
        $data = [
            'pid' => $pid['id'],
            'address' => $address
        ];
        if (!empty($address)) {
            return JsonTools::JsonSuccess($data);
        }
    }

    /**
     * @Notes:
     * @Author: 遇见
     * @Date: 2022/11/16
     * @Time: 15:01
     * @return array
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     * 获取所有区
     */
    public function regionAddress()
    {
        $param = request()->get('id');
        $address = (new HouseServices())->addressRegion($param);
        return $address;
    }

    /**
     * @Notes:
     * @Author: 遇见
     * @Date: 2022/11/16
     * @Time: 15:01
     * 获取所有市
     */
    public function userProvince()
    {
        $address = (new HouseServices())->addressProvince();
        return $address;
    }

    /**
     * @Notes:
     * @Author: 遇见
     * @Date: 2022/11/16
     * @Time: 18:37
     * 获取用户手机号
     */
    public function userPhone()
    {
        $param = request()->get('uid');
        $phone = (new HouseServices())->userPhone($param);
        return $phone;
    }


    /**
     * @Notes:获取附近房源
     * @Author:Planb,
     * @Date: 2022/11/22,
     * @Time: 10:56,
     */
    public function getNearbyHouse()
    {
        $latitude = request()->get('latitude');
        $longitude = request()->get('longitude');
        $distance = request()->get('distance');
        $price = request()->get('price');
        $price = conditionService::price($price);
        $distance = conditionService::distance($distance);
        $type = request()->get('type', 2);
        if ($type == 1) {
            $type = 'buy';
        } else {
            $type = 'rent';
        }
        $addressResult = (new RedisGeo())->mapFindHouse($latitude, $longitude, $distance, $type, $price);
        if ($addressResult) {
            return JsonTools::JsonSuccess($addressResult, 200, 'ok');
        } else {
            return JsonTools::JsonError([], 8001, '附近无房源');
        }
    }

    /**
     * @Notes:
     * @Author: 遇见
     * @Date: 2022/11/26
     * @Time: 8:29
     * @return array
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     * 获取房源的信息
     */
    public function listHouse()
    {
        $param = request()->get('id');
        $model = (new HouseServices())->houseList($param);
        return $model;
    }


    /**
     * @Notes:收藏房源
     * @Author:Planb,
     * @Date: 2022/12/2,
     * @Time: 19:53,
     */
    public function likeHouse(Request $request){
        $uid = \request()->id;
        $houseId = request('house_id');
        if (!is_numeric($houseId)){
            return JsonTools::JsonError([],6001,'参数错误');
        }
        $result = HouseServices::likeHouseInfo($uid,$houseId);
        switch ($result){
            case StatusNumber::UPDATESUCCESS:
                return JsonTools::JsonSuccess([],200,'取消收藏');
                break;
            case StatusNumber::UPDATEFAIL:
                return JsonTools::JsonError([],12001,'取消收藏');
                break;
            case StatusNumber::LIKESUCCESS:
                return JsonTools::JsonSuccess([],200,'收藏成功');
                break;
            case StatusNumber::LIKEFAIL:
                return JsonTools::JsonError([],12001,'收藏失败');
                break;
        }
    }

}
